#include "searchdialog.h"
#include "ui_searchdialog.h"

#include <QtGui/QAbstractItemView>
#include <QDesktopWidget>
#include <QMessageBox>
#include <QDebug>

SearchDialog::SearchDialog(QAbstractItemView *searchWidget, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SearchDialog),
    m_model(0),
    m_currentRow(0),
    m_tableView(searchWidget)
{
    ui->setupUi(this);

    if(m_tableView) {
        m_model = m_tableView->model();
        int colCount = m_model->columnCount();
        for(int i = 0; i < colCount; i++) {
            ui->columnsCombo->insertItem(ui->columnsCombo->count(), m_model->headerData(i, Qt::Horizontal).toString());
        }
        ui->columnsCombo->insertItem(ui->columnsCombo->count(), "Все столбцы");
        ui->columnsCombo->setCurrentIndex(ui->columnsCombo->count()-1);
    }

#ifdef Q_OS_ANDROID
    setWindowFlags(Qt::ToolTip);
    QRect rect = geometry();
    int height = rect.height();
    rect = QApplication::desktop()->screenGeometry();
    rect.setHeight(height);
    rect.setY(0);
    setGeometry(rect);
#else
    ui->closeButton->hide();
#endif
}

SearchDialog::~SearchDialog()
{
    delete ui;
}

void SearchDialog::on_searchButton_clicked()
{
    Q_ASSERT(m_model);
    QModelIndexList indexList;
    QModelIndex index;
    QString searchStr = ui->lineEdit->text();
    QFlags<Qt::MatchFlag> matchFlag;
    matchFlag |= Qt::MatchFixedString;

    if( ui->checkExactly->isChecked() )
        matchFlag |= Qt::MatchExactly;
    else
        matchFlag |= Qt::MatchContains;

    if( ui->checkCaseSens->isChecked() )
        matchFlag |= Qt::MatchCaseSensitive;

    int row = m_model->rowCount()+1, col = -1;

    if( ui->columnsCombo->currentIndex() == ui->columnsCombo->count() - 1 ) {
        for(int i = 0; i < m_model->columnCount(); i++) {
            indexList = m_model->match( m_model->index(m_currentRow, i), Qt::DisplayRole, searchStr, 1, matchFlag );
            if(indexList.size() != 0) {
                int indexRow = indexList.at(0).row();
                if( indexRow < row && indexRow >= m_currentRow ) {
                    row = indexRow;
                    col = i;
                    index = indexList.at(0);
                }
            }
        }
    } else {
        indexList = m_model->match( m_model->index(m_currentRow, ui->columnsCombo->currentIndex()), Qt::DisplayRole, searchStr, 1, matchFlag );
        if(indexList.size() != 0) {
            int indexRow = indexList.at(0).row();
            if( indexRow < row && indexRow >= m_currentRow ) {
                row = indexRow;
                col = ui->columnsCombo->currentIndex();
                index = indexList.at(0);
            }
        }
    }

    if(col >= 0) {
        m_currentRow = row + 1;
        m_tableView->selectionModel()->setCurrentIndex( index, QItemSelectionModel::ToggleCurrent );
    } else {
        int res = QMessageBox::question(m_tableView, "Поиск "+searchStr, "Достигнут конец списка. Начать поиск с начала?", QMessageBox::Yes, QMessageBox::No);
        if(res == QMessageBox::Yes) {
            m_currentRow = 0;
            on_searchButton_clicked();
        }
    }

}

void SearchDialog::on_lineEdit_textChanged(const QString &/*arg1*/)
{
    m_currentRow = 0;
}
